<template>
  <div>
    <el-form ref="ruleForm" style="margin-top: 30px;" :model="music" label-width="100px">
      <el-form-item label="音乐名称" prop="musicname">
        <el-input v-model="music.name" />
      </el-form-item>
      <el-form-item label="歌手" prop="musicname">
        <el-input v-model="music.artist" />
      </el-form-item>
      <el-form-item label="封面" prop="cover">
        <el-upload
          v-loading="imgLoading"
          element-loading-text="图片上传中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="#fff"
          element-
          class="avatar-uploader"
          :action="uploadUrl"
          :headers="headers"
          :show-file-list="false"
          :on-success="handleUploadSuccess"
          :before-upload="imageUploading"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
      </el-form-item>
      <el-form-item label="歌曲文件上传" prop="url">
        <el-upload
          class="upload-demo"
          :action="uploadUrl"
          :headers="headers"
          :on-success="musicUploadSuccess"
          :file-list="fileList"
          :before-upload="fileUploading"
        >
          <el-button :loading="fileLoading" element-loading-spinner="el-icon-loading" size="small" type="primary">点击上传</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item label="歌词" prop="musicEmail">
        <el-input v-model="music.lrc" type="textarea" />
      </el-form-item>
      <el-form-item label-width="100px">
        <el-button type="primary" @click="submitForm">提交</el-button>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
import musicApi from '@/api/music'
export default {
  name: 'MusicAdd',
  data() {
    return {
      music: {
      },
      headers: {
        Authorization: getToken()
      },
      imageUrl: '',
      uploadUrl: process.env.VUE_APP_UPLOAD_URL,
      dateValue: new Date(),
      imgLoading: false,
      fileList: [
      ],
      fileLoading: false
    }
  },
  created() {
  },
  methods: {
    submitForm() {
      const loading = this.$loading({
        lock: true,
        text: '添加中请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      musicApi.save(this.music).then(res => {
        if (res.code === 200) {
          this.music = res.data
          loading.close()
          this.$message.success(res.msg)
          this.$emit('closeMusicAddDialog')
          this.$emit('getMusicList')
        } else {
          this.$message.success(res.msg)
          loading.close()
        }
      }).catch(err => {
        console.log(err)
        loading.close()
      })
    },
    handleUploadSuccess(res, file) {
      this.imageUrl = res.data
      this.music.cover = res.data
      this.imgLoading = false
      this.$message.success(res.msg)
    },
    // 歌曲文件上传
    musicUploadSuccess(val) {
      this.music.url = val.data
      this.fileLoading = false
      this.$message.success(val.msg)
    },
    imageUploading() {
      this.imgLoading = true
    },
    fileUploading() {
      this.fileLoading = true
    }
  }
}
</script>

<style scoped>
  .avatar-uploader {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    background-size: cover;
    display: block;
  }
</style>

